package com.changhongit.webservice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.changhongit.dao.ServiceProviderDao;
import com.changhongit.dao.ServiceTypeDao;
import com.changhongit.entity.ServiceType;
import com.changhongit.returnentity.ReturnServiceProvider;

@Controller
public class WebService {
	@Autowired
	private ServiceProviderDao serviceProviderService;
	
	@Autowired
	private ServiceTypeDao serviceTypeService;

	@RequestMapping(value = "/queryServiceProviders", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getServiceProviders(
			ReturnServiceProvider returnservice) {
		Map<String, Object> map = new HashMap<>();
		String distance = returnservice.getDistance();
		try{
			if (distance != null && !"".equals(distance)) {
				distance = String.valueOf(Integer.valueOf(distance));
			}else{
				distance = "10";
			}
			returnservice.setDistance(distance);
			List<ReturnServiceProvider> serviceProviders = serviceProviderService
					.findAllServiceProviders(returnservice);
			map.put("code", 200);
			map.put("serviceProviders", serviceProviders);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return map;
	}
	
	@RequestMapping(value = "/queryAllServiceType", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAllServiceType() {
		Map<String, Object> map = new HashMap<>();
		List<ServiceType> servicetypes = serviceTypeService.findAll();
		map.put("code", 200);
		map.put("servicetypes", servicetypes);
		return map;
	}
	
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> test() {
		Map<String, Object> map = new HashMap<>();
		serviceProviderService.getById("14");
		map.put("code", 200);
		return map;
	}

}
